<template>
  <div class="ele-body">
    <!-- 搜索表单 -->
    <div class="block-interval">
      <a-card :bordered="false">
        <a-form layout="inline" :model="where">
          <a-row>
            <a-form-item label="所属牧场:" name="orgId">
              <a-select
                show-search
                v-model:value="where.orgId"
                placeholder="请选择所属牧场"
                style="width: 205px"
                :show-arrow="false"
                :filter-option="false"
                :not-found-content="null"
                allow-clear
              >
                <a-select-option v-for="item in ranchList" :key="item.ranchId" >
                  {{ item.ranchName }}
                </a-select-option>
              </a-select>
            </a-form-item>

            <a-form-item class="ele-text-center">
              <a-space>
                <a-button type="primary" @click="reload">查询</a-button>
                <a-button @click="reset">重置</a-button>
              </a-space>
            </a-form-item>

          </a-row>
        </a-form>
      </a-card>
    </div>

    <!-- 表格 -->
    <div>
      <a-card :bordered="false">
        <ele-pro-table
          ref="table"
          row-key="oxtallId"
          :datasource="datasource"
          :columns="columns"
          :where="where"
          :scroll="{ x: 'max-content' }"
        >

          <template #bodyCell="{ column, record }">
            <!-- table操作栏按钮 -->
            <template v-if="column.key === 'action'">
              <a-space>
                <a v-permission="['MANAGERDEVICE_SPRAYCAMERAGROUP_ALLOCATION_BUTTON']" @click="openEdit(record)">设备分区</a>
                <a-divider type="vertical" v-any-permission-and="[['MANAGERDEVICE_SPRAYCAMERAGROUP_AREA_BIAOZHU_BUTTON', 'MANAGERDEVICE_SPRAYCAMERAGROUP_SCHEDULE_CONFIG_EDIT_BUTTON','MANAGERDEVICE_SPRAYCAMERAGROUP_DEL_SCHEDULE_BUTTON'],['MANAGERDEVICE_SPRAYCAMERAGROUP_ALLOCATION_BUTTON']]" />
                <div v-permission="['MANAGERDEVICE_SPRAYCAMERAGROUP_DEL_SCHEDULE_BUTTON']">
                   <a-popconfirm title="确定要移除调度配置吗？" @confirm="removeSprayConfig(record)">
                    <a class="ele-text-danger">移除调度配置</a>
                  </a-popconfirm>
                </div>
                <a-divider type="vertical" v-any-permission-and="[['MANAGERDEVICE_SPRAYCAMERAGROUP_AREA_BIAOZHU_BUTTON', 'MANAGERDEVICE_SPRAYCAMERAGROUP_SCHEDULE_CONFIG_EDIT_BUTTON'],['MANAGERDEVICE_SPRAYCAMERAGROUP_DEL_SCHEDULE_BUTTON', 'MANAGERDEVICE_SPRAYCAMERAGROUP_ALLOCATION_BUTTON']]" />
                <a v-permission="['MANAGERDEVICE_SPRAYCAMERAGROUP_SCHEDULE_CONFIG_EDIT_BUTTON']" @click="openConfigEdit(record)">喷淋配置</a>
                <a-divider type="vertical" v-any-permission-and="[['MANAGERDEVICE_SPRAYCAMERAGROUP_AREA_BIAOZHU_BUTTON'],['MANAGERDEVICE_SPRAYCAMERAGROUP_SCHEDULE_CONFIG_EDIT_BUTTON', 'MANAGERDEVICE_SPRAYCAMERAGROUP_DEL_SCHEDULE_BUTTON', 'MANAGERDEVICE_SPRAYCAMERAGROUP_ALLOCATION_BUTTON']]" />
                <a v-permission="['MANAGERDEVICE_SPRAYCAMERAGROUP_AREA_BIAOZHU_BUTTON']" @click="openBiaozhu(record)">镜头标注</a>
                <a-divider type="vertical" v-any-permission="['MANAGERDEVICE_SPRAYCAMERAGROUP_AREA_BIAOZHU_BUTTON', 'MANAGERDEVICE_SPRAYCAMERAGROUP_SCHEDULE_CONFIG_EDIT_BUTTON', 'MANAGERDEVICE_SPRAYCAMERAGROUP_DEL_SCHEDULE_BUTTON', 'MANAGERDEVICE_SPRAYCAMERAGROUP_ALLOCATION_BUTTON']" />
                <a @click="manager(record)">分区数据管理</a>
              </a-space>
            </template>
          </template>
        </ele-pro-table>
      </a-card>
    </div>

    <DeviceManagerdeviceCameraAssignmentDis v-if="showEdit" v-model:visible="showEdit" :data="current" @done="reload" />
    <DeviceManagerdeviceCameraAssignmentConfig v-if="showConfig" v-model:visible="showConfig" :data="current" @done="reload" />
    <DeviceManagerdeviceCameraAssignmentBiaozhu v-if="showBiaozhu" v-model:visible="showBiaozhu" :data="current" @done="reload" />
    
    <component :is="currentView" v-if='showManagerModal' v-model:visible="showManagerModal" :data="current" />
  </div>
</template>

<script>
import { OxtallApi } from '@/api/property/OxtallApi';
import { RanchApi } from '@/api/property/RanchApi';
import { DeviceSprayCameraScheduleConfigApi } from '@/api/property/spraycamera/DeviceSprayCameraScheduleConfigApi';
import DeviceManagerdeviceCameraAssignmentDis from './camera-assignment-dis.vue'
import DeviceManagerdeviceCameraAssignmentConfig from './camera-assignment-config.vue'
import DeviceManagerdeviceCameraGroupmanagerModal from './camera-groupmanager-modal.vue'
import DeviceManagerdeviceCameraAssignmentBiaozhu from './camera-assignment-biaozhu.vue'
import { message } from 'ant-design-vue';
export default {
  name: 'DeviceManagerdeviceCameraAssignment',
  components: {
    DeviceManagerdeviceCameraAssignmentDis,
    DeviceManagerdeviceCameraAssignmentConfig,
    DeviceManagerdeviceCameraGroupmanagerModal,
    DeviceManagerdeviceCameraAssignmentBiaozhu
  },
  async mounted() {
    RanchApi.list({}).then(resp => {
      if (resp && resp.data &&  resp.data.length > 0) {
        this.ranchList = resp.data;
      }
    });
  },
  data() {
    return {
      // 表格列配置
      columns: [
        {
          title: '牛舍名字',
          dataIndex: 'oxtallName'
        },
        {
          title : '所属牧场',
          dataIndex: 'ranchName',
        },
        {
          title: '创建时间',
          dataIndex: 'createTime',
          width: 150,
        },
        {
          title: '操作',
          key: 'action',
          width: 450,
          align: 'center'
        }
      ],
      // 表格搜索条件
      where: {},
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
      ranchList : [],
      showConfig : false,
      showManagerModal : false,
      currentView : '',
      showBiaozhu : false,
    };
  },
  methods: {
    manager(row) {
      this.currentView = 'DeviceManagerdeviceCameraGroupmanagerModal';
      this.current = row;
      this.showManagerModal = true;
    },
    /**
     * 搜索按钮
     *
     * @author cancan
     * @date 2022/03/02 22:49
     */
    reload() {
      this.selection = [];
      this.$refs.table.reload({ page: 1 });
    },

    /**
     * 重置搜索
     *
     * @author cancan
     * @date 2022/03/02 22:49
     */
    reset() {
      this.where.orgId = '';
      this.reload();
    },

    async removeSprayConfig(row) {
      const result = await DeviceSprayCameraScheduleConfigApi.disableConfig({ oxtallId: row.oxtallId });
      message.success(result.message);
    },

    /**
     * 打开新增或编辑弹窗
     *
     * @author cancan
     * @date 2022/03/02 22:49
     */
    openEdit(row) {
      this.current = row;
      this.showEdit = true;
    },

    openConfigEdit(row) {
      this.current = row;
      this.showConfig = true;
    },

    openBiaozhu(row) {
      this.current = row;
      this.showBiaozhu = true;
    },

    /**
     * 获取表格数据
     *
     * @author fengshuonan
     * @date 2022/5/8 15:18
     */
    datasource({ page, limit, where, orders }) {
      return OxtallApi.findPage({ ...where, ...orders, pageNo: page, pageSize: limit });
    }
  }
};
</script>
